#include "SudokuCell.h"

SudokuCell::SudokuCell()
{
    value = 0;
    prior = Empty;
    possiblArr = QBitArray(1, true);//TODO
}

SudokuCell::SudokuCell(int maxValue)
{
    value = 0;
    prior = Empty;
    possiblArr = QBitArray(maxValue, true);
}

//TODO maxValue not needed?
SudokuCell::SudokuCell(int maxValue, int _value, int _prior)
{
    value = _value;
    prior = _prior;
    //TODO possiblArr stays empty
    //possiblArr = QBitArray(maxValue, true);
}

SudokuCell::SudokuCell(const SudokuCell& cell)
{
    value = cell.value;
    prior = cell.prior;
    possiblArr = cell.possiblArr;//TODO copy?
}

SudokuCell::~SudokuCell()
{
}

SudokuCell& SudokuCell::operator=(const SudokuCell& cell)
{
    if (&cell == this)
        return *this;

    value = cell.value;
    prior = cell.prior;
    possiblArr = cell.possiblArr;//TODO copy?

    return *this;
}

void SudokuCell::setValue(int _value, int _prior)
{
    value = _value;
    prior = _prior;
    int arrSize = possiblArr.size();
    possiblArr.fill(false, arrSize);
    //clean possiblArr???
    //TODO
}

int SudokuCell::getValue() const
{
    return value;
}

int SudokuCell::getPrior() const
{
    return prior;
}

void SudokuCell::setPossibl(int index, bool possibl)
{
    if(index < possiblArr.size())
        possiblArr.setBit(index,possibl);
}

bool SudokuCell::getPossibl(int index) const
{
    return possiblArr.at(index);
}

int SudokuCell::countPossible() const
{
    //if(!isEmpty() && possiblArr.count(true) > 0)
    //    qFatal("SudokuCell::countPossible wrong!!!!");//TODO
    return possiblArr.count(true);
}

int SudokuCell::getPossiblArrSize()
{
    return possiblArr.size();
}

bool SudokuCell::isEmpty() const
{
    return (prior == Empty);
}

void SudokuCell::clear()
{
    value = 0;
    prior = Empty;
    int possiblLength = possiblArr.size();
    possiblArr.fill(true, possiblLength);
}

QDebug operator<<(QDebug dbg, const SudokuCell& cell)
{
    dbg.nospace() << "[" << cell.value << "(";//"[" << cell.value << "(" << cell.prior << "){";
    for(int i=0;i<cell.possiblArr.size();++i)
        if(cell.possiblArr.testBit(i))
            dbg.nospace() << i+1;// << ",";
    dbg.nospace() << ")]";
    return dbg.maybeSpace();
}

bool SudokuCell::hasMoreClues(const SudokuCell* cell1, const SudokuCell* cell2)
{
    //TODO check filled cells
    return (cell1->countPossible() > cell2->countPossible());
}

bool SudokuCell::hasLessClues(const SudokuCell* cell1, const SudokuCell* cell2)
{
    //TODO check filled cells
    return (cell1->countPossible() < cell2->countPossible());
}
